package com.xianyu.game.Contorller;

import com.xianyu.game.Pojo.IdIntegrates;
import com.xianyu.game.Pojo.Result;
import com.xianyu.game.Pojo.User;
import com.xianyu.game.Service.Impl.UserServiceImpl;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
@CrossOrigin
@RestController
@Tag(name="用户接口")
@RequestMapping("/users")
public class UserContorller {
    @Autowired
    private UserServiceImpl usi;

    /**
     * 登录
     * @param user
     * @return
     */
    @Operation(summary = "登录")
    @PostMapping("/login")
    public Result login(@RequestBody User user){
        if(usi.login(user)){
            return Result.success();
        }else {
            return Result.error("登录失败");
        }
    }

    /**
     * 注册
     * @param user
     * @return
     */
    @Operation(summary = "注册")
    @PostMapping("/registered")
    public Result registered(@RequestBody User user){
        //判断该用户是否存在
        if(!usi.isExist(user)){
            //如果不存在,向数据库添加用户
            usi.registered(user);
            return Result.success();
        }else{
            return Result.error("注册失败，该用户已存在");
        }
    }

    /**
     * 获取用信息
     * @param userId
     * @return
     */
    @Operation(summary = "获取用户信息")
    @PostMapping("/getUser")
    public Result getUser(@RequestParam @Parameter(description = "用户Id") String userId){
        return Result.success(usi.getUser(userId));
    }

    /**
     * 修改用户信息
     * @param user
     * @return
     */
    @Operation(summary = "修改用户信息")
    @PostMapping("/updateUser")
    public Result updateUser(@RequestBody User user){
        usi.revise(user);
        return Result.success();
    }

    /**
     * 购买游戏
     * @param idIntegrates
     * @return
     */
    @Operation(summary = "购买游戏")
    @PostMapping("/buyGame")
    public Result buyGame(@RequestBody IdIntegrates idIntegrates){
        //查询是否已经购买过该游戏
        if(usi.isBuy(idIntegrates)){
            return Result.error("该游戏已存在库中");
        }else {
            usi.buyGame(idIntegrates);
            return Result.success();
        }
    }

    /**
     * 获取库中的游戏
     * @param userId
     * @return
     */
    @Operation(summary = "获取库中的游戏")
    @GetMapping("/getGame")
    public Result getGame(@RequestParam @Parameter(description = "用户Id") Integer userId){
        return Result.success(usi.getGame(userId));
    }


    /**
     * 收藏游戏
     * @param idIntegrates
     * @return
     */
    @Operation(summary = "收藏游戏")
    @PostMapping("collectionGame")
    public Result collectionGame(@RequestBody IdIntegrates idIntegrates){
        //查询该游戏是否已经收藏过
        if (usi.isCollection(idIntegrates)){
            return Result.error("该游戏已经收藏了");
        }else{
            //收藏游戏
            usi.collectionGame(idIntegrates);
            return Result.success();
        }
    }

    /**
     * 获取收藏的游戏
     * @param userId
     * @return
     */
    @Operation(summary = "获取收藏的游戏")
    @GetMapping("/getCollection")
    public Result getConllection(@RequestParam @Parameter(description = "用户Id") Integer userId){
        return Result.success(usi.getCollection(userId));
    }

    /**
     * 购物车
     * @param idIntegrates
     * @return
     */
    @Operation(summary = "将游戏加入购物车")
    @PostMapping("/shoppingCart")
    public Result addShoppingCart(@RequestBody IdIntegrates idIntegrates){
        //查询该游戏是否存在购物车
        if (usi.isExisCart(idIntegrates)){
            return Result.error("该游戏已存在购物车");
        }else {
            usi.addCart(idIntegrates);
            return Result.success();
        }
    }

}
